package test.demo;
/**
 * <p>开闭原则示例二(优化后)</p>
 * */
public class OcpNew {
    public static void main(String[] args) {
        GraphicEditorNew graphicEditor = new GraphicEditorNew();
        graphicEditor.drawShape(new TriangleNew());
        graphicEditor.drawShape(new OtherGraphicNew());
    }
}
//这是一个用于绘图的类 [使用方]
class GraphicEditorNew {
    //接收Shape对象，调用draw方法
    public void drawShape(ShapeBaseNew s) {
        s.draw();
    }
}
//Shape类，基类
abstract class ShapeBaseNew {
    int m_type;
    public abstract void draw();//抽象方法
}
//新增画三角形
class TriangleNew extends ShapeBaseNew {
    TriangleNew() { super.m_type = 3; }
    @Override
    public void draw() { System.out.println(" 绘制三角形 "); }
}
//新增一个图形
class OtherGraphicNew extends ShapeBaseNew {
    OtherGraphicNew() { super.m_type = 4; }
    @Override
    public void draw() { System.out.println(" 绘制其它图形 "); }
}

